package org.apogee.mil.client.main.pages.admin.forms.widgets;

import java.util.ArrayList;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.main.pages.admin.forms.group.GroupConfigurationComponent;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.shared.Constants.DisplayFormat;
import org.apogee.mil.shared.Constants.GroupFields;
import org.apogee.mil.shared.User;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;

public class Groups implements IsWidget {
	private User currentUser;
	private ColumnModel cm;
	private Grid<BaseModel> grid;
	private ListStore<BaseModel> groupStore = new ListStore<BaseModel>();
	private GroupConfigurationComponent parentWidget;

	public Groups(GroupConfigurationComponent parentWidget) {
		this.currentUser = ServerServiceFactory.getUser(Session.getSessionId());
		this.parentWidget = parentWidget;
	}

	@Override
	public Widget asWidget() {
		ContentPanel cp = new ContentPanel();
		cp.setSize(275, 400);
		ServerServiceFactory.getAdminService(currentUser.getSessionId()).getGroups(new AsyncCallback<ArrayList<BaseModel>>() {

			@Override
			public void onSuccess(ArrayList<BaseModel> result) {
				if (result.size() == 0) {
					MessageBox.alert("Error", "Unable to get Group Data", null);
					return;
				}

				groupStore.removeAll();
				groupStore.add(result);
			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert("Error", "Unable to get Group Data\n"+caught.getMessage(), null);

			}
		});
		GridCellRenderer<BaseModel> renderer = new GridCellRenderer<BaseModel>() {

			@Override
			public Object render(BaseModel model, String property, ColumnData config,
					int rowIndex, int colIndex, ListStore<BaseModel> store,
					Grid<BaseModel> grid) {

				String val = (String) (model.get(property) == null ? "No Groups..." : model.get(property));
				return "<span qtitle='" + cm.getColumnById(property).getHeader() + "' qtip='" + val  
						+ "' style='font-weight: bold;color: #5d8aa8'>" + val + "</span>";
			}
		};

		ArrayList<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		ColumnConfig column = new ColumnConfig();
		column.setId(DisplayFormat.GROUP_NAME_FMT);
		column.setHeader("Available Groups");
		column.setWidth(270);
		column.setRowHeader(true);
		column.setRenderer(renderer);
		configs.add(column);

		cm = new ColumnModel(configs);

		grid = new Grid<BaseModel>(groupStore, cm);
		grid.setBorders(true);
		grid.setColumnLines(true);
		grid.getAriaSupport().setLabelledBy(cp.getHeader().getId()+"-label");
		grid.setSize(275, 335);
		grid.addListener(Events.CellClick, new Listener<GridEvent<BaseModel>>(){

			@Override
			public void handleEvent(GridEvent<BaseModel> be) {
				String groupId = be.getModel().get(GroupFields.GROUP_ID);
				parentWidget.update(groupId);
			}

		});
		cp.add(grid);
		
		return cp;
	}
	
	public ListStore<BaseModel> getStore() {
		return groupStore;
	}

}
